package com.kanshu.ksgb.fastread.doudou.module.reader.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.supertextview.SuperTextView;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.MMKVUserManager;
import com.kanshu.ksgb.fastread.doudou.module.reader.utils.SettingManager;
import com.kanshu.ksgb.fastread.doudou.module.reader.utils.ThemeManager;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.fragment.AdBookCityFragment;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 书籍错误处理页面（包括下线和章节错误）
 * 作者    wcy
 * 时间    2018/11/19 9:37
 * 文件    doudou
 * 描述
 */
public class BookReadErrorLayout extends FrameLayout {
    @BindView(R.id.back_to_book_city)
    SuperTextView mBackToBookCity;
    @BindView(R.id.offline_container)
    LinearLayout mOfflineContainer;
    @BindView(R.id.retry)
    SuperTextView mRetry;
    @BindView(R.id.chapter_error_container)
    LinearLayout mChapterErrorContainer;
    ReadErrCallback mCallback;
    public BookReadErrorLayout(@NonNull Context context) {
        super(context);
        init();
    }

    public BookReadErrorLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BookReadErrorLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void init() {
       View v = LayoutInflater.from(getContext()).inflate(R.layout.layout_book_error_handle_item, this);
        ButterKnife.bind(v);
        setWillNotDraw(true);
        OnTouchListener listener = (v1, event) -> {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (mCallback != null) {
                    mCallback.onTouch();
                }
            }
            return true;
        };
        mChapterErrorContainer.setOnTouchListener(listener);
        mOfflineContainer.setOnTouchListener(listener);
    }

    public void setCallback(ReadErrCallback callback) {
        mCallback = callback;
    }

    @OnClick({R.id.back_to_book_city, R.id.retry,/*R.id.offline_container, R.id.chapter_error_container*/})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_to_book_city:
                int subtab = AdBookCityFragment.TYPE_TAB_SELECTED;
                if (MMKVUserManager.getInstance().getLoginSex().equals(String.valueOf(Constants.MALE))) {
                    subtab =  Constants.BookCityTabType.TYPE_MALE;
                } else if (MMKVUserManager.getInstance().getLoginSex().equals(String.valueOf(Constants.FEMALE))) {
                    subtab =  Constants.BookCityTabType.TYPE_FEMALE;
                }
                Map<String, String> map = new HashMap<>();
                map.put("tab_index", ARouterConfig.HomePageParams.TAB_BOOK_CITY + "");
                map.put("sub_tab_index", subtab + "");
                ARouterUtils.toActivity(ARouterConfig.HOME_PAGE, map);
                break;
            case R.id.retry:
                if (mCallback != null) {
                    mCallback.onRetry();
                }
                break;
//            case R.id.chapter_error_container:
//                break;
//            case R.id.offline_container:
//                break;
        }
    }

    /**
     * 刷新页面
     * @param code
     */
    public void refreshByErrorCode(int code) {
        DisplayUtils.visible(this);
        if (code == Constants.ErrCode.Offline) {
            DisplayUtils.visible(mOfflineContainer);
            DisplayUtils.gone(mChapterErrorContainer);
        } else if (code == Constants.ErrCode.NetErr) {
            DisplayUtils.gone(mOfflineContainer);
            DisplayUtils.visible(mChapterErrorContainer);
            ThemeManager.setReaderTheme(SettingManager.getInstance().getReadTheme(), mChapterErrorContainer);
        }
    }

    public interface ReadErrCallback {
        void onRetry();
        void onTouch();
    }
}
